#include <sys/mman.h>

#define HUGE_PAGE_SIZE 0x200000 // 2MiB Page Size
#define PAGE_SIZE 0x1000

#ifdef HAS_NCE
#define VIRTUAL_RESERVE_SIZE (1ULL << 38)
#else
#define VIRTUAL_RESERVE_SIZE (1ULL << 39)
#endif

__attribute__((visibility("default"))) void*  virtual_base = MAP_FAILED;
__attribute__((visibility("default"))) size_t virtual_last = VIRTUAL_RESERVE_SIZE;

static __attribute__((constructor, visibility("hidden"))) void preload() {
    for (uintptr_t i = PAGE_SIZE; i <= HUGE_PAGE_SIZE && MAP_FAILED == virtual_base; i <<= 1)
        virtual_base = mmap((void*)i, virtual_last - i, PROT_NONE,
                            MAP_NORESERVE | MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_ANON, -1, 0);
}